package com.zky.DAO.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zky.DA0.Imp1Dao;
import com.zky.common.HibernateSessionFactory;
import com.zky.entity.Imp1;


public class Imp1DaoImp implements Imp1Dao {
	/* (non-Javadoc)
	 * @see com.zky.DAO.impl.Imp1Dao#delete(int)
	 */
	public void delete(int id) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
		    Imp1 imp = (Imp1)session.get(Imp1.class, id);
		    session.delete(imp);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }
		}

		
		/* (non-Javadoc)
		 * @see com.zky.DAO.impl.Imp1Dao#findAll()
		 */
		@SuppressWarnings("unchecked")
		public List<Imp1> findAll() {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   List<Imp1> list = new ArrayList<Imp1>();
		   try{
		    String hql = "from Imp1";
		    Query query=session.createQuery(hql);
		    list=query.list();
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }
		   return list;
		}

		/* (non-Javadoc)
		 * @see com.zky.DAO.impl.Imp1Dao#findById(int)
		 */
		public Imp1 findById(int id) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
			   Imp1 imp = (Imp1)session.createQuery("from Imp1 s where s.id='"+id+"'").uniqueResult();
		    //Student student=(Student) session.get(Student.class, id);
		    transaction.commit();
		    return imp;
		   }catch(Exception e){
		    e.printStackTrace();
		    transaction.rollback();
		   }finally{
		    session.close();
		   }
		   return null;
		}

		
		/* (non-Javadoc)
		 * @see com.zky.DAO.impl.Imp1Dao#save(com.zky.entity.Imp1)
		 */
		public void save(Imp1 imp) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
		    session.save(imp);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }
		}

		
		/* (non-Javadoc)
		 * @see com.zky.DAO.impl.Imp1Dao#update(com.zky.entity.Imp1)
		 */
		public void update(Imp1 imp) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
			   
		    session.update(imp);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }

		}


		@SuppressWarnings("unchecked")
		@Override
		public List<Imp1> findAll(String name) {
			Session session = HibernateSessionFactory.getSession();
			Transaction transaction = session.beginTransaction();
			 List<Imp1> list = new ArrayList<Imp1>();
			 try{
				String hql="from Imp1 s where s.type='"+name+"'";
				 Query query=session.createQuery(hql);
				  list=query.list();
				  transaction.commit();				
				   }catch(Exception e){
				    e.printStackTrace();
				   }finally{
				    session.close();
				   }
			return list;

			
					   
					   
		}


}
